package com.adou.quartz;

import com.adou.quartz.action.QuartzManager;
import com.adou.quartz.task.Task;
import com.adou.quartz.task.TaskGroupBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Order(value = 1)
public class ScheduleJobInitListener implements CommandLineRunner {

    @Autowired
    TaskGroupBase taskGroupBase;
    @Autowired
    QuartzManager quartzManager;

    private static final Logger logger = LoggerFactory.getLogger(ScheduleJobInitListener.class);

    @Override
    public void run(String... arg0) throws Exception {
        try {
            List<Task> list = taskGroupBase.getTasks();
            for (Task task : list) {
                if (task.getState() == 1) {
                    quartzManager.addTask(task);
                }
            }
        } catch (Exception e) {
            logger.error(e.toString());
            e.printStackTrace();
        }
    }
}
